import scoreNumberPickerTpl from './scoreNumberPicker.html';

function scoreNumberPickerComponent() {
  'ngInject';

  let directive = {
    restrict: 'E',
    replace: true,
    templateUrl: scoreNumberPickerTpl,
    controller: scoreNumberPickerController,
    controllerAs: 'scoreNumberPickerCtrl',
    bindToController: {
      scoreType: '@', // 扣分类型
      scoreLower: '@', // 扣分下限
      scoreUpper: '@', // 扣分上限
      ngScore: '=', // 扣分数,传递给父组件
    },
  };

  function scoreNumberPickerController($scope) {
    'ngInject';

    this.problemCount = 0;
    this.ngScore = 0;

    // 获得按分值扣分的viewmodel
    let getScoreList = () => {
      let scoreList = [];
      scoreList.push({
        label: '不扣分',
        value: 0,
      });

      let floatScore = this.scoreLower;
      while (floatScore <= this.scoreUpper) {
        scoreList.push({
          label: `扣${floatScore}分`,
          value: floatScore,
        });
        floatScore++;
      }
      return scoreList;
    }

    if (this.scoreType == 1) {
      this.scoreList = getScoreList();
    }
    

    // 计算按次数扣分分值
    this.getScoreByCount = () => {
      this.ngScore = this.problemCount * this.scoreLower;
    }

    this.add = () => {
      this.problemCount++;
    }

    this.reduce = () => {
      if (this.problemCount <= 0) {
        this.canReduce = false;
        return;
      }
      this.problemCount--;
    }

    $scope.$watch('scoreNumberPickerCtrl.problemCount', this.getScoreByCount);

  }

  return directive;
}

export default scoreNumberPickerComponent;
